$(function() {
	// 初始化日期控件
	$.suredy.datetimepicker.init();

	// 合同工期提示
	$('.contract-days-helper').tooltip({
		container : 'body',
		delay : {
			"show" : 200,
			"hide" : 100
		},
		trigger : 'hover',
		placement : 'right',
		title : function() {
			var text = '【开工日期、竣工日期】与【工期天数】只能选择一类进行填写！';
			return text;
		}
	});

	// 提交合同信息
	$('a.submit-contract-info').on('click', function() {
		var form = $('form.form-create-contract');

		var cStart = $('[name="contractStart"]').val();
		var cEnd = $('[name="contractEnd"]').val();
		var cDays = $('[name="contractDays"]').val();

		if (!cStart && !cEnd && !cDays) {
			alert('【开工日期、竣工日期】与【工期天数】必须选择一类进行填写！');
			return false;
		} else if (!cDays) {
			if (!cStart || !cEnd) {
				alert('开工日期与竣工日期必须同时填写！');
				return false;
			}
		} else {
			if (cStart || cEnd) {
				alert('【开工日期、竣工日期】与【工期天数】只能选择一类进行填写！');
				return false;
			}
		}

		// 校验表单
		if (!$.suredy.formChecker(form)) {
			return false;
		}

		$.suredy.showProcessDialog({
			keyboard : false,
			text : '<div class="text-danger"><i class="icon-spinner icon-spin"></i>&nbsp;数据提交中，请稍候……</div>'
		});

		// 修复[买标书费用]、[投标保证金]的资金去向
		$('.money-source').val($('input[name="otherCompany"]').val());

		form.ajaxSubmit({
			dataType : "json",
			success : function(data, textStatus, jqXHR) {
				if (!data || !data.success) {
					alert(data.msg || '合同信息保存失败!');
					$.suredy.closeProcessDialog();
					return false;
				}

				window.location.href = $.suredy.contextPath + '/contract/view/' + data.data;
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('合同信息保存失败!\n' + errorThrown);
				$.suredy.closeProcessDialog();
				return false;
			}
		});
	});

	// 重置表单
	$('a.reset-form-info').on('click', function() {
		var form = $('form.form-create-contract');

		if (form.length > 0) {
			form[0].reset();
		}
	});

	// 增加甲方单位
	$('.btn.bidding-create').on('click', function() {
		$.suredy.showModal({
			icon : 'icon-plus',
			title : '新建设单位',
			uri : $.suredy.contextPath + '/config/biddinger/create',
			okClick : function() {
				var form = $('form.form-add-biddinger');

				// 校验表单
				if (!$.suredy.formChecker(form)) {
					return false;
				}

				form.ajaxSubmit({
					success : function(data, textStatus, jqXHR) {
						if (!data) {
							alert('增加建设单位失败！');
							return false;
						}

						var select = $('select[name="companyA"]');
						select.append('<option value="' + data.company + '">' + data.company + '</option>');

						$('option', select).last().prop('selected', true);

						$.suredy.closeModal();
					},
					error : function(jqXHR, textStatus, errorThrown) {
						alert('增加建设单位失败！\n' + errorThrown);
						return false;
					}
				});
			}
		});
	});

	// 返回浏览
	$('a.back-view').on('click', function() {
		var $this = $(this);
		var id = $this.data('id');

		console.log(id);

		window.location.href = $.suredy.contextPath + '/contract/view/' + id;
	});

});